package com.own.core.html.panel;

import java.lang.reflect.Field;

import javax.persistence.Transient;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.html.form.FormComponent;

import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;


import com.own.core.html.form.Label.ComponentVisualErrorBehavior;
import com.own.core.html.form.Label.OwnFeedbackLabel;
import com.own.core.html.form.Label.OwnLabel;
import com.own.core.html.panel.OwnPanel;
import com.own.util.ResourceUtil;

public class OwnDynPanel extends OwnPanel {
	@Transient
	private FormComponent formComponent;
	@Transient
	IModel model;
	@Transient
	Field field;
	boolean enableFeedBack = true;

	public FormComponent getFormComponent() {
		return formComponent;
	}

	public void setFormComponent(FormComponent formComponent) {
		this.formComponent = formComponent;

	}

	private OwnDynPanel(String id, IModel model) {
		super(id, model);

	}

	public OwnDynPanel(IModel model, Field field) {

		this.model = model;
		this.field = field;
		name = field.getName();
	}

	public void draw() {

		this.add(formComponent);
		formComponent.setOutputMarkupId(true);
		formComponent.setLabel(ResourceUtil.getResource(field));

		this.add(new OwnLabel("label", ResourceUtil.getResource(field)));

		if (enableFeedBack) {
			OwnFeedbackLabel feedback = new OwnFeedbackLabel("feedback",
					formComponent);
			feedback.setOutputMarkupId(true);
			this.add(feedback);

			formComponent.add(new ComponentVisualErrorBehavior("onblur",
					feedback));
		}
	}

	public void setRequired(boolean required) {
		formComponent.setRequired(required);
		if (required) {
			formComponent.add(new AttributeModifier("class", true, new Model(
					"required")));
		}

	}

	public IModel getModel() {
		return model;
	}

	public Field getField() {
		return field;
	}

	public void setModel(IModel model) {
		this.model = model;
	}

	public void setField(Field field) {
		this.field = field;
	}

	public boolean isEnableFeedBack() {
		return enableFeedBack;
	}

	public void setEnableFeedBack(boolean enableFeedBack) {
		this.enableFeedBack = enableFeedBack;
	}

}
